package com.servlet;

import com.dao.ProductDAO;
import com.dao.impl.ProductDAOImpl;
import com.entity.Product;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.text.ParseException;

@WebServlet("/insert")
public class InsertServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");

        // 获取请求参数
        String productId = req.getParameter("productId");
        String productName = req.getParameter("productName");
        String productType = req.getParameter("productType");
        String productPrice = req.getParameter("productPrice");
        String productStock = req.getParameter("productStock");
        String productImg = req.getParameter("productImg");
        String productDate = req.getParameter("productDate");
        String productDesc = req.getParameter("productDesc");
        String productSales = req.getParameter("productSales");
        String productStatus = req.getParameter("productStatus");

        // 创建商品对象
        Product product = new Product();

        // 设置商品属性
        try {
            product.setId(productId); // 商品ID
            product.setName(productName); // 商品名称
            product.setType(productType); // 商品类型
            product.setPrice(Double.parseDouble(productPrice)); // 商品价格
            product.setCount(Integer.parseInt(productStock)); // 商品库存
            product.setImage(productImg); // 商品图片
            product.setDesc(productDesc); // 商品描述
            product.setSale(Integer.parseInt(productSales)); // 商品销量
            product.setStatus(Integer.parseInt(productStatus)); // 商品状态

            // 处理日期格式
            if (productDate != null && !productDate.isEmpty()) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                java.util.Date utilDate = sdf.parse(productDate); // 转换为 java.util.Date
                java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime()); // 转换为 java.sql.Date
                product.setDate(sqlDate); // 设置商品上架日期
            } else {
                resp.getWriter().write("{\"code\":400, \"message\":\"日期格式无效或为空\"}");
                return;
            }

        } catch (NumberFormatException | ParseException e) {
            e.printStackTrace();
            resp.getWriter().write("{\"code\":400, \"message\":\"请求参数无效\"}");
            return;
        }

        // 使用 DAO 插入商品数据
        ProductDAO dao = new ProductDAOImpl();
        int result = dao.addProduct(product);

        // 返回响应结果
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter out = resp.getWriter();
        if (result > 0) {
            out.print("{\"code\":200, \"message\":\"商品添加成功\"}");
        } else {
            out.print("{\"code\":500, \"message\":\"商品添加失败\"}");
        }
    }
}
